import data_rdb from '@ohos.data.relationalStore'
import { OperatorType } from './OperatorType'

/**
 * 查询条件构造器
 */
export default class PredicatesBuilder {
  private predicates: data_rdb.RdbPredicates

  constructor(tableName: string) {
    this.predicates = new data_rdb.RdbPredicates(tableName)
  }

  /**
   * 等于
   * @param filedName
   * @param value
   * @returns
   */
  eq(filedName: string, value) {
    return this.addCondition(filedName, OperatorType.EQUAL_TO, value)
  }

  /**
   * 添加构建天剑
   * @param fiedName
   * @param type
   * @param values
   */
  addCondition(fiedName: string, type: OperatorType | string, values: any[]) {
    let result: string = type.toString().toUpperCase()

    switch (result) {
      case OperatorType.EQUAL_TO:
        this.predicates.equalTo(fiedName, values[0])
        break
      case OperatorType.NOT_EQUAL_TO:
        this.predicates.notEqualTo(fiedName, values[0])
        break
      case OperatorType.BEGIN_WRAP:
        this.predicates.beginWrap()
        break
      case OperatorType.END_WRAP:
        this.predicates.endWrap()
        break
      case OperatorType.OR:
        this.predicates.or()
        break
      case OperatorType.AND:
        this.predicates.and()
        break
      case OperatorType.CONTAINS:
        this.predicates.contains(fiedName, values[0])
        break
      case OperatorType.BEGINS_WITH:
        this.predicates.beginsWith(fiedName, values[0])
        break
      case OperatorType.ENDS_WITH:
        this.predicates.endsWith(fiedName, values[0])
        break
      case OperatorType.IS_NULL:
        this.predicates.isNull(fiedName)
        break
      case OperatorType.IS_NOT_NULL:
        this.predicates.isNotNull(fiedName)
        break
      case OperatorType.LIKE:
        this.predicates.like(fiedName, values[0])
        break
      case OperatorType.GLOB:
        this.predicates.glob(fiedName, values[0])
        break
      case OperatorType.BETWEEN:
        this.predicates.between(fiedName, values[0], values[1])
        break
      case OperatorType.NOT_BETWEEN:
        this.predicates.notBetween(fiedName, values[0], values[1])
        break
      case OperatorType.GREATER_THAN:
        this.predicates.greaterThan(fiedName, values[0])
        break
      case OperatorType.LESS_THAN:
        this.predicates.lessThan(fiedName, values[0])
        break
      case OperatorType.GREATER_THAN_OR_EQUAL_TO:
        this.predicates.greaterThanOrEqualTo(fiedName, values[0])
        break
      case OperatorType.LESS_THAN_OR_EQUAL_TO:
        this.predicates.lessThanOrEqualTo(fiedName, values[0])
        break
      case OperatorType.ORDER_BY_ASC:
        this.predicates.orderByAsc(fiedName)
        break
      case OperatorType.ORDER_BY_DESC:
        this.predicates.orderByDesc(fiedName)
        break
      case OperatorType.DISTINCT:
        this.predicates.distinct()
        break
      case OperatorType.LIMIT_AS:
        this.predicates.limitAs(values[0])
        break
      case OperatorType.OFFSET_AS:
        this.predicates.offsetAs(values[0])
        break
      case OperatorType.GROUP_BY:
        this.predicates.groupBy(values)
        break
      case OperatorType.INDEXED_BY:
        this.predicates.indexedBy(fiedName)
        break
      case OperatorType.IN:
        this.predicates.in(fiedName, values)
        break
      case OperatorType.NOT_IN:
        this.predicates.notIn(fiedName, values)
        break
    }
    return this
  }

  build() {
    return this.predicates
  }
}